<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\PortalCategoryModel;
use cmf\controller\HomeBaseController;

class ListController extends HomeBaseController
{
    public function index()
    {
        $id                  = $this->request->param('id', 0, 'intval');
        $portalCategoryModel = new PortalCategoryModel();

        $category = $portalCategoryModel->where('id', $id)->where('status', 1)->find();
        $this->assign('category', $category);

        $categoryIds = substr(str_replace('-', ',', $category->path), 2);
        $this->assign('categoryIds', $this->getSonCateIds($id));
        $fatherCIds = explode(',', $categoryIds);
        $fatherCId = $fatherCIds[1] ?? $fatherCIds[0];
        $this->assign('fatherCId', $fatherCId);
        $listTpl = empty($category['list_tpl']) ? 'list' : $category['list_tpl'];

        $this->assign("title", $category['seo_title'] ? $category['seo_title'] : $category['name'] );
        $this->assign("keyword", $category['seo_keywords']);
        $this->assign("description", $category['seo_description']);


        // 获取这个pid 下面的子分类
//        $sonCateList = $portalCategoryModel->where("status = 1 and parent_id = ". $pid)->order("list_order", "desc")->select();
        list($sonCateList,$parentCategory) = $this->getCategory($category);
        $this->assign("sonCateList", $sonCateList);
        $this->assign("parentCategory", $parentCategory);

        return $this->fetch('/' . $listTpl);
    }

    private function getSonCateIds($fid) {
        $model = new PortalCategoryModel();
        $list = $model->whereLike("path", "%{$fid}-%")->select()->toArray();
        if(empty($list)) {
            return $fid;
        }
        return $fid.','.implode(",", array_column($list, 'id'));
    }
    public function getCategory($category) {
        $path = $category['path'];
        $pidsArr = explode("-", $path);
        $pid = $pidsArr[1] ?? $pidsArr[0];
        // 获取他的子分类
        $categoryModel = new PortalCategoryModel();
        $parentCate = $categoryModel->find($pid);

        $sonList = $categoryModel->where("parent_id = $pid and status = 1 and delete_time = 0")->order("list_order", "asc")->select()->toArray();
        foreach ($sonList as &$cate) {
            $cate['son'] = $categoryModel->where("status = 1 and parent_id = ".$cate['id']. " and delete_time = 0")->order("list_order", "asc")->select();
        }
        return [$sonList, $parentCate];
    }
}
